<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */

App::uses('AppController', 'Controller');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class PagesController extends AppController {

/**
 * This controller does not use a model
 *
 * @var array
 */
	public $uses = array();

/**
 * Displays a view
 *
 * @param mixed What page to display
 * @return void
 * @throws NotFoundException When the view file could not be found
 *	or MissingViewException in debug mode.
 */
 
	public function home(){
		$date_current = date('Y-m-d H:i:s');
		$this->loadModel('Reservation');
		$this->loadModel('Room');
		$this->loadModel('Storey');
		$this->loadModel('Cateroom');
		$this->loadModel('Guest');
		$this->Reservation->recursive = 1;
		$arr_demo = array();

		// Danh sách phòng đang ở
		$arr_id_cur = array();
		$current = $this->Reservation->find('all', array(
			'conditions' => array('Reservation.status' => 1),
			'contain' => array('Users')
		));
		foreach ($current as $v1) {
			array_push($arr_id_cur,$v1['Reservation']['rooms_id']);
			$arr_demo[$v1['Reservation']['rooms_id']] = $v1['Reservation']['id'];
		}

		// Danh sách phòng đang đặt
		$arr_id_book = array();
		$booking = $this->Reservation->find("all", array(
			"conditions" => array("AND" => array("Reservation.status" => 2,
				"Reservation.start >=" => $date_current
			))
		));
		foreach ($booking as $v2) {
			if(in_array($v2['Reservation']['rooms_id'], $arr_id_book)){
				continue;
			}
			array_push($arr_id_book,$v2['Reservation']['rooms_id']);
		}
		$arr_id_book = array_diff($arr_id_book, $arr_id_cur);

		// Danh mục loại phòng, lầu, phòng (tất cả)
		$room_storey = $this->Storey->find('all');
		$room_cate = $this->Cateroom->find('all');
		$rooms = $this->Room->find('all');
		$this->Guest->recursive = -1;

		// Danh sách khách hàng chờ xếp phòng
		if(!isset($_GET['pw']) || $_GET['pw'] <= 0){
			$pw = 1;
		}else{
			$pw = $_GET['pw'];
		}
		$all_gw = $this->Guest->find('list', array('conditions' => array('Guest.status' => 0)));
		$sum_gw = count($all_gw);
		$n = 2;
		$start1 = $n*$pw-$n;
		$query_gw = "SELECT id, name, cmt, gioitinh, address, phone FROM guests WHERE status= 0 ORDER BY id ASC LIMIT $start1, $n";
		$guest_waiting = $this->Guest->query($query_gw);
		$page1 = ceil($sum_gw/$n);
		
		// Danh sách khách hàng đang ở
		if(!isset($_GET['pc']) || $_GET['pc'] <= 0){
			$pc = 1;
		}else{
			$pc = $_GET['pc'];
		}
		$all_gc = $this->Reservation->find('list', array(
			'conditions' => array('Reservation.status' => 1),
			'group' => array('Reservation.guest_id'),
			'fields' => array('Reservation.guest_id')
		));
		$sum_gc = count($all_gc);
		$start2 = $n*$pc-$n;
		$guest_curr = $this->Guest->find('all', array(
			'conditions' => array('Guest.id' => $all_gc),
			'fields' => array('Guest.id', 'Guest.name', 'Guest.cmt', 'Guest.gioitinh', 'Guest.address', 'Guest.phone'),
			'order' => array('Guest.id' => 'asc'),
			'limit' => $n,
			'offset'=> $start2
		));
		$page2 = ceil($sum_gc/$n);

		// Danh sách khách hàng đang đặt phòng
		if(!isset($_GET['pb']) || $_GET['pb'] <= 0){
			$pb = 1;
		}else{
			$pb = $_GET['pb'];
		}
		$all_gb = $this->Reservation->find('list', array(
			'conditions' => array('Reservation.status' => 2),
			'group' => array('Reservation.guest_id'),
			'fields' => array('Reservation.guest_id')
		));
		$sum_gb = count($all_gb);
		$start3 = $n*$pb-$n;
		$guest_book = $this->Guest->find('all', array(
			'conditions' => array('Guest.id' => $all_gb),
			'fields' => array('Guest.id', 'Guest.name', 'Guest.cmt', 'Guest.gioitinh', 'Guest.address', 'Guest.phone'),
			'order' => array('Guest.id' => 'asc'),
			'limit' => $n,
			'offset'=> $start3
		));
		$page3 = ceil($sum_gb/$n);

		$this->set(compact('rooms','sum_gw','sum_gb','sum_gc','page1','page2','page3','guest_waiting','guest_book','guest_curr','arr_id_book','arr_id_cur','current','booking','room_storey','room_cate','arr_demo'));
	}
	public function display() {
		$path = func_get_args();

		$count = count($path);
		if (!$count) {
			return $this->redirect('/');
		}
		$page = $subpage = $title_for_layout = null;

		if (!empty($path[0])) {
			$page = $path[0];
		}
		if (!empty($path[1])) {
			$subpage = $path[1];
		}
		if (!empty($path[$count - 1])) {
			$title_for_layout = Inflector::humanize($path[$count - 1]);
		}
		$this->set(compact('page', 'subpage', 'title_for_layout'));

		try {
			$this->render(implode('/', $path));
		} catch (MissingViewException $e) {
			if (Configure::read('debug')) {
				throw $e;
			}
			throw new NotFoundException();
		}
	}
}
